package com.artisan.util;

import java.io.*;

public class FileUtil {
  /*
   * Java文件操作 获取文件扩展名
   *
   *  Created on: 2011-8-2
   *      Author: blueeagle
   */
  public static String getExtensionName(String filename) {
    if ((filename != null) && (filename.length() > 0)) {
      int dot = filename.lastIndexOf('.');
      if ((dot > -1) && (dot < (filename.length() - 1))) {
        return filename.substring(dot + 1);
      }
    }
    return filename;
  }
  /*
   * Java文件操作 获取不带扩展名的文件名
   *
   *  Created on: 2011-8-2
   *      Author: blueeagle
   */
  public static String getFileNameNoEx(String filename) {
    if ((filename != null) && (filename.length() > 0)) {
      int dot = filename.lastIndexOf('.');
      if ((dot > -1) && (dot < (filename.length()))) {
        return filename.substring(0, dot);
      }
    }
    return filename;
  }

  public static void saveFile(String filePath, InputStream fileInputStream) {
    FileOutputStream os = null;
    try {
      File tempFile = new File(filePath);
      if (!tempFile.exists()) {
        os = new FileOutputStream(filePath);
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;

        // 开始读取
        while ((len = fileInputStream.read(bs)) != -1) {
          os.write(bs, 0, len);
        }
      }



    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // 完毕，关闭所有链接
      try {
        os.close();
        fileInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
